#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys,os,time

sys.path.append(os.path.realpath('../../'))

os.environ['DJANGO_SETTINGS_MODULE'] = 'websth.settings'

from resource.models import *
from member.models import *
from item.models import *
from multiprocessing import Lock
from config import *


LOCK = Lock()

def update_piece(args):
    global LOCK
    _type = args['type']
    _feed = args['feed']
    feed_start = time.time()
    result = urllib2.urlopen(_feed.Url)
    feed_parsed = feedparser.parse(result.read())
    try:
        LOCK.acquire()
        FEED_FACTORY_DICT[_type].update(_feed, feed_parsed)
    except Exception, e:
        print '[-] Error: %s' % e
    finally:
        LOCK.release()
        print '[+] Update %s ok.' % _feed.Name
        print '[*] Run Time: ' + str(time.time() - feed_start) + 'secs.'

def update():
    """
    update feed by Database key , if key is empty then update all feeds
    """

    
    # Update all feeds which not updated UPDATE_INTERVAL seconds ago
    # 这里做了分批更新，不全部更新，而是根据最后更新时间判断，超过半小时没更新的才进行更新
    feeds = Feed.objects.filter(updateDate__lte = datetime.now() - timedelta(seconds = UPDATE_INTERVAL))
    args = [{'type': feed.Type, 'feed':feed} for feed in feeds]
    
    # start process pool
    from multiprocessing import Pool
    
    pool = Pool(PROCESS_POOL_MAX)
    pool.map_async(update_piece, args)

def main():
    start = time.time()
    try:
        update()
    except Exception, e:
        print '[-] Error: %s' %e
    finally:
        end = time.time()
        print '[*] Total Run Time: ' + str(end - start) + 'secs.'
      
#必须从命令行启动
if "__main__" == __name__: main()